﻿using System;
using System.Globalization;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Developer.Web.Models;
using System.Collections.Generic;
using Super.Framework.Common.Web;
using Developer.Service;
using System.Collections;
using Developer.Code;
using Super.Framework.Common.Secure;
using ifunction.YunPian.SDK;
using Newtonsoft.Json.Linq;

namespace Developer.Web.Controllers
{
    
    public class AccountController : BaseController
    {
        public ActionResult Register()
        {
            return View();
        }

        public ActionResult Login()
        {
            return View();
        }


        /// <summary>
        /// 检查账户
        /// </summary>
        /// <returns>
        /// Json 0：登录失败    1：登陆成功，并返回userId
        /// </returns>
        [HttpPost]
        public JsonResult CheckAccount()
        {
            Hashtable result = new Hashtable();
            var account = Query.Value("phone");
            var pwd = Query.Value("pwd").Md5String();

            var user = UserService.GetAll().FirstOrDefault(a => a.Account == account && a.IsAbort == false);
            if (user == null)
            {
                result.Add(0, "用户不存在");
                return Json(new { error = "用户不存在" }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                if(user.Password != pwd)
                {
                    result.Add(0, "密码错误");
                    return Json(new { error = "密码错误" }, JsonRequestBehavior.AllowGet);
                }
                result.Add(1, user.Id);
            }
            return Json(new { success = user.Id}, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 忘记密码
        /// </summary>
        /// <returns></returns>
        public ActionResult ForgetPassword()
        {
            return View();
        }
       
        public ActionResult FeedBack()
        {
            var uid = Query.Value("uid").ToInt();
            if(uid > 0)
            {
                var f = FeedBackService.GetAll().Where(a => a.UserId == uid);
                return View(f);
            }
            return View();
        }
        [HttpPost]
        public ActionResult FeedBack(int? z)
        {
            var uid = Query.Value("uid").ToInt();
            var content = Query.Value("content");
            if(uid != 0 && content != null)
            {
                FeedBackService.AddFeedBack(content, uid);
            }
            
            return Redirect("/Account/FeedBack?uid="+uid);
        }

        #region 重置密码短信验证码
        [HttpPost]
        public JsonResult ResetPassword()
        {
            var phone = Query.Value("phone");
            Hashtable result = new Hashtable();

            var code = SMSService.GetValidCode(100000, 999999);
            var content = "【橙砸软件】您的验证码是" + code + "。有效期为3分钟，请尽快验证";
            try
            {
                var rs = SMSService.SendSms(content, phone);
                //JObject obj = JObject.Parse(rs);

                if (rs.Code.ToInt() == 0)
                {
                    ValidCodeService.ResetPwdSetValidCode(phone, code.ToString());
                    result.Add("1", "发送成功");
                }
                else
                {
                    result.Add("0", "发送失败");
                }

            }
            catch (Exception e)
            {
                result.Add("0", e.ToString());
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }


        [HttpPost]
        public JsonResult ResetPasswordValid()
        {
            var phone = Query.Value("phone");
            var code = Query.Value("code");
            var pwd = Query.Value("password").Md5String();
            var result = ValidCodeService.ResetPwdValidCode(phone, code);
            switch (result)
            {
                case 0:
                    if (UserService.GetAll().FirstOrDefault(a => a.Account == phone).IsEmpty())
                    {
                        return Json(new { error = "该用户未注册" }, JsonRequestBehavior.AllowGet);
                    }
                    else
                    {
                        UserService.ResetPassword(phone,pwd);
                        return Json(new { success = "修改成功！" }, JsonRequestBehavior.AllowGet);
                    }
                case 1:
                    return Json(new { error = "验证码超时" }, JsonRequestBehavior.AllowGet); 
                case 2:
                    return Json(new { error = "验证码错误" }, JsonRequestBehavior.AllowGet);
                default: return Json(new { error = "当前号码未发送验证码" }, JsonRequestBehavior.AllowGet);
            }
        }

#endregion


        #region 发送验证码
        /// <summary>
        /// 发送验证码
        /// </summary>
        /// <returns>
        /// Json result  1: 发送成功 0: 发送失败 2: 其他故障
        /// </returns>
        [HttpPost]
        public JsonResult SendSms()
        {
            var phone = Query.Value("phone");
            Hashtable result = new Hashtable();

            var code = SMSService.GetValidCode(100000, 999999);
            //var content = "【蚁行软件】您的验证码是"+code+"。有效期为3分钟，请尽快验证";
            var content = "【橙砸软件】您的验证码是"+code+"。有效期为3分钟，请尽快验证";
            try
            {
                var rs = SMSService.SendSms(content, phone);
                //JObject obj = JObject.Parse(rs);
            
                if (rs.Code.ToInt() == 0)
                {
                    ValidCodeService.SetValidCode(phone, code.ToString());
                    result.Add("1", "发送成功");
                }
                else
                {
                    result.Add("0", "发送失败");
                }

            }
            catch (Exception e)
            {
                result.Add("0", e.ToString());
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }
        
        [HttpPost]
        public JsonResult ValidateCode()
        {
            var phone = Query.Value("phone");
            var code = Query.Value("code");
            var pwd = Query.Value("password").Md5String();
            var result = ValidCodeService.ValidCode(phone, code);
            Hashtable rs = new Hashtable();
            switch (result)
            {
                case 0:
                    if(!UserService.GetAll().FirstOrDefault(a=>a.Account == phone).IsEmpty())
                    {
                        rs.Add("0", "用户已存在");
                    }
                    else
                    {
                        UserService.Add(new Entity.Users
                        {
                            IsAbort = false,
                            Account = phone,
                            CreateTime = DateTime.Now,
                            NickName = "起个名字咯",
                            HeadImg = "/Content/UI/images/head.jpg",
                            Password = pwd,
                            IsUseWechat = false,
                            RoleId = 1

                        });
                        rs.Add("1", "注册成功!");
                    }
                    break;
                case 1:
                    rs.Add("0", "验证码超时"); break;
                case 2:
                    rs.Add("0", "验证码错误"); break;
                default: rs.Add("0", "当前号码未发送验证码"); break;
            }
            return Json(rs,JsonRequestBehavior.AllowGet);
        }

       
#endregion

    }
}